package com.lablzh.gdxy.service.impl;

import com.lablzh.gdxy.component.BCryptPasswordEncoderUtil;
import com.lablzh.gdxy.component.UserDetailsUtil;
import com.lablzh.gdxy.entity.*;
import com.lablzh.gdxy.repository.*;
import com.lablzh.gdxy.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class InfoServiceImpl implements InfoService {
    @Autowired
    StudentRepositoty studentRepositoty;
    @Autowired
    TeacherRepository teacherRepository;
    @Autowired
    AdminRepository adminRepository;
    @Autowired
    InstructorRepository instructorRepository;
    @Autowired
    private UserDetailsUtil userDetailsUtil;
    @Override
    public Student getStudentInfo() {
        String username=userDetailsUtil.getUserDetails();
        return studentRepositoty.findAllByStID(username);
    }
    @Override
   public Page<Student> querystudentSelective(Integer page, Integer limit)
    {
        Pageable pageable = PageRequest.of(page, limit, Sort.by(Sort.Direction.DESC,"id"));
        //Specification<Goods> specification = getListSpecification(typeDtl);
        return studentRepositoty.findAll( pageable);
    }
    @Override
    public String putStudentInfo(Student student) {
       if(studentRepositoty.save(student)!=null){
           return "修改成功";
       }else {
           return "修改失败";
       }
    }

    @Override
    public Teacher getTeacherInfo() {
        String username=userDetailsUtil.getUserDetails();
        return teacherRepository.findAllByUsername(username);
    }

    @Override
    @CacheEvict(cacheNames = "teacher",allEntries = true)
    public String putTeacherInfo(Teacher teacher) {
        if(teacherRepository.save(teacher)!=null){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }



    @Override
    public Admin getAdminInfo() {
        String username =userDetailsUtil.getUserDetails();
        System.out.println("用户名"+username);
        return adminRepository.findAllByUsername(username);
    }



    @Override
    public String putAdminInfo(Admin admin) {
        if(adminRepository.save(admin)!=null){
            return "修改成功";
        }else {
            return "修改失败";
        }
}
    @Override
    public Instructor getInstructorInfo() {
        String username=userDetailsUtil.getUserDetails();
        return instructorRepository.findAllByUsername(username);
    }

    @Override
    public String putInstructorInfo(Instructor instructor) {
        if(instructorRepository.save(instructor)!=null){
            return "修改成功";
        }else {
            return "修改失败";
        }
    }
    @Override
    public Admin getAdminInfoByUsername(String username) {
        return adminRepository.findAllByUsername(username);
    }

    @Override
    public Student getStudentInfoByUsername(String username) {
      return studentRepositoty.findAllByStID(username);
    }

    @Override
    @Cacheable(cacheNames = "teacher")
    public Teacher getTeacherInfoByUsername(String username) {
        return teacherRepository.findAllByUsername(username);
    }
    @Override
    public Instructor getInstructorInfoByUsername(String username) {
        return instructorRepository.findAllByUsername(username);
    }

    /**
     * 获取所有的教师
     * @return
     */
    @Override
    @Cacheable(cacheNames = "teacher")
    public List<Teacher> getAllTeacher() {
        return teacherRepository.findAll(Sort.by(Sort.Direction.DESC,"id"));
    }



    /**
     *
     * @return
     */


    @Override
    public List<Student> getAllStudent() {
        return studentRepositoty.findAll();
    }
    @Override
    public List<Admin> getAllAdmin() {
        return adminRepository.findAll();
    }
    @Override
    public List<Instructor> getAllInstructor() {
        return instructorRepository.findAll();
    }
}
